package com.lovo.pis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.pis.dto.BuyCarGoodsBean;
import com.lovo.pis.dto.CustomBean;
import com.lovo.pis.dto.PageBean;
import com.lovo.pis.dto.SellCarGoodsBean;
import com.lovo.pis.mapper.BuyCarGoodsMapper;
import com.lovo.pis.mapper.CustomMapper;
import com.lovo.pis.service.IBuyCarGoodsService;
import com.lovo.pis.service.ICustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BuyCarGoodsServiceImpl extends ServiceImpl<BuyCarGoodsMapper, BuyCarGoodsBean> implements IBuyCarGoodsService {

    @Autowired
    BuyCarGoodsMapper buyCarGoodsMapper;


    @Override
    public IPage<BuyCarGoodsBean> queryAllBuyCarGoods(PageBean<BuyCarGoodsBean> pageInfo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("car_id",pageInfo.getData().getCarId());
        return buyCarGoodsMapper.selectPage(new Page<>(pageInfo.getCurPage(),pageInfo.getPageSize()),queryWrapper);
    }

    @Override
    public void changeItem(int carId,int id, int number, int price) {
        buyCarGoodsMapper.updateItem(carId,id,number,price);

    }

    @Override
    public void removeById(int carId, int id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("car_id",carId);
        queryWrapper.eq("id",id);
        buyCarGoodsMapper.delete(queryWrapper);
    }

    @Override
    public void addBuyCarGoods(BuyCarGoodsBean buyCarGoodsBean) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",buyCarGoodsBean.getId());
        queryWrapper.eq("car_id",buyCarGoodsBean.getCarId());
        BuyCarGoodsBean goodsBean = buyCarGoodsMapper.selectOne(queryWrapper);
        if(goodsBean!=null){
            goodsBean.setNumber(goodsBean.getNumber()+buyCarGoodsBean.getNumber());
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("car_id",buyCarGoodsBean.getCarId());
            updateWrapper.eq("id",buyCarGoodsBean.getId());
            buyCarGoodsMapper.update(goodsBean,updateWrapper);
        }else{
            buyCarGoodsMapper.insert(buyCarGoodsBean);
        }
    }

}
